Skip to main content

Kotlin Basics

  • Nullable checks - nulls are only done explicitly
var a: String = "abc"
a = null // compilation error

var b: String? = "abc"
b = null // ok
print(b)
  • Mandatory nulls - the below will throw an error if it is null
val l = b!!.length
  • Null safe same as groovy

bob?.department?.head?.name

Data class provides by default:

data class Customer(val name: String, val email: String)

provides a Customer class with the following functionality:

getters (and setters in case of vars) for all properties

equals()

hashCode()

toString()

copy()

component1(), component2(), …, for all properties (see Data classes)

If not null;

val files = File("Test").listFiles()

println(files?.size ?: "empty")

Execute if not null:

val value = ...

value?.let {
... // execute this block if not null
}

Sealed classes can have subclasses but only within the file

Val is final Var is not